package ua.at.tsvetkov.samizdat.adapters;

import java.util.Date;

import ua.at.tsvetkov.strings.Util;
import ua.at.tsvetkov.util.Log;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import ua.at.tsvetkov.samizdat.Const;
import ua.at.tsvetkov.samizdat.R;
import ua.at.tsvetkov.samizdat.data.Data;
import ua.at.tsvetkov.samizdat.data.DataLoadedAuthorsBooks;
import ua.at.tsvetkov.samizdat.data.FavoriteAuthors;
import ua.at.tsvetkov.samizdat.data.FavoriteBooks;

public class AdtLoadedBooks extends BaseAdapter {

	private LayoutInflater				mInflater;
	private DataLoadedAuthorsBooks	dataLoadedAuthorsBooks;
	private FavoriteAuthors	favoriteAuthors;
	private FavoriteBooks	favoriteBooks;

	public AdtLoadedBooks(Activity context, DataLoadedAuthorsBooks dataLoadedAuthorsBooks) {
		this.dataLoadedAuthorsBooks = dataLoadedAuthorsBooks;
		this.mInflater = LayoutInflater.from(context);
		favoriteAuthors = new FavoriteAuthors(context);
		favoriteAuthors.load();
		favoriteBooks = new FavoriteBooks(context);
		favoriteBooks.load();	}

	public void refresh() {
		dataLoadedAuthorsBooks.load();
		favoriteAuthors.clear();
		favoriteBooks.clear();
		favoriteAuthors.load();
		favoriteBooks.load();
	}

	@Override
	public int getCount() {
		return dataLoadedAuthorsBooks.booksData.length;
	}

	@Override
	public Object getItem(int arg0) {
		return dataLoadedAuthorsBooks.booksData[arg0];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		BookViewHolder holder = new BookViewHolder();
		if (convertView == null || convertView.getTag() != holder) {
			convertView = mInflater.inflate(R.layout.adt_loaded_book, null);
			holder.favoriteBook = (ImageView) convertView.findViewById(R.id.AdtLoadedBookIsFavoriteComposition);
			holder.favoriteAuthor = (ImageView) convertView.findViewById(R.id.AdtLoadedBookIsFavoriteAuthor);
			holder.author = (TextView) convertView.findViewById(R.id.AdtLoadedBookAuthor);
			holder.title = (TextView) convertView.findViewById(R.id.AdtLoadedBookTitle);
			holder.type = (TextView) convertView.findViewById(R.id.AdtLoadedBookType);
			holder.size = (TextView) convertView.findViewById(R.id.AdtLoadedBookSize);
			holder.raiting = (TextView) convertView.findViewById(R.id.AdtLoadedBookRating);
			holder.data = (TextView) convertView.findViewById(R.id.AdtLoadedBookData);
			holder.isNew = (TextView) convertView.findViewById(R.id.AdtLoadedBookIsNew);
			convertView.setTag(holder);
		} else {
			holder = (BookViewHolder) convertView.getTag();
		}
		try {
			String[] fields = Util.split(dataLoadedAuthorsBooks.booksData[position], '|');
			holder.author.setText(fields[Data.HEADER_AUTHOR]);
			holder.title.setText(fields[Data.HEADER_TITLE].trim());
			holder.type.setText(fields[Data.HEADER_TYPE]);
			String size = fields[Data.HEADER_SIZE];
			if (size != null && size.length() != 0) {
				holder.size.setText(size);
			}
			String ball = fields[Data.HEADER_BALL];
			if (ball != null && ball.length() != 0) {
				holder.raiting.setText(fields[Data.HEADER_COUNTER] + '*' + ball);
			}
			if (fields[Data.HEADER_DATE].length() > 0) {
				Date date = new Date(fields[Data.HEADER_DATE]);
				if (date.after(Const.CONTROL_DATE)) {
					holder.isNew.setText("New!");
				}
				holder.data.setText(fields[Data.HEADER_DATE]);
			} else {
				holder.isNew.setText("New!");
			}
			String object = fields[Data.HEADER_URL].substring(fields[Data.HEADER_URL].indexOf("object=") + 7);
			if (favoriteBooks.isFavorite(object)) {
				holder.favoriteBook.setVisibility(View.VISIBLE);
			}
			if (favoriteAuthors.isFavorite(fields[Data.HEADER_AUTHOR])) {
				holder.favoriteAuthor.setVisibility(View.VISIBLE);
			}
			} catch (Exception e) {
			Log.e("Line " + position + ":" + dataLoadedAuthorsBooks.booksData[position], e);
		}
		return convertView;
	}

	class BookViewHolder {
		ImageView	favoriteAuthor;
		ImageView	favoriteBook;
		TextView		author;
		TextView		title;
		TextView		type;
		TextView		size;
		TextView		raiting;
		TextView		data;
		TextView		isNew;
	}
}
